package com.mindata.blockchain.socket.handler.server;

import com.mindata.blockchain.ApplicationContextProvider;
import com.mindata.blockchain.block.Block;
import com.mindata.blockchain.common.ConsensusType;
import com.mindata.blockchain.core.event.AddBlockEvent;
import com.mindata.blockchain.socket.base.AbstractBlockHandler;
import com.mindata.blockchain.socket.body.RpcBlockBody;
import com.mindata.blockchain.socket.packet.BlockPacket;
import org.tio.core.ChannelContext;

/**
 * @Author:wenhui
 * @Description: RAFT-block落地处理器，发送AddBlockEvent即可
 * @Date:Create in 2018-7-31 14:19
 **/
public class CommitRaftBlockRequestHandler extends AbstractBlockHandler<RpcBlockBody> {
    @Override
    public Class<RpcBlockBody> bodyClass() {
        return RpcBlockBody.class;
    }

    @Override
    public Object handler(BlockPacket packet, RpcBlockBody rpcBlockBody, ChannelContext channelContext) throws Exception {
        Block block = rpcBlockBody.getBlock();
        ApplicationContextProvider.publishEvent(new AddBlockEvent(ConsensusType.RAFT,block));
        return null;
    }
}
